<?php

namespace App\Http\Controllers;

use App\Models\Config;
use Illuminate\Http\Request;

class ConfigController extends Controller
{
    public function index(Request $request)
    {
        $pageIndex = $request->get('pageIndex');
        $pageSize = $request->get('pageSize');

        $configs = Config::query()
            ->orderByDesc('created_at')
            ->paginate($pageSize, ['*'], 'pageIndex', $pageIndex);

        return $this->response($configs);
    }

    //
    public function store(Request $request)
    {
        $keys = $request->keys;

        $configs = Config::query()->whereIn('key',$keys)->pluck('val','key')->toArray();

        return $this->response($configs);
    }

    public function update($id, Request $request)
    {
        $newConfig = $request->all();

        $config = Config::query()->where('id', $id)->first();

        if (!$config) return $this->response([],'参数未配置',500);

        $config->val = $newConfig['val'];

        $config->save();

        return $this->response($config,'修改成功');
    }

    public function destroy($id)
    {
        $config = Config::query()->where('id', $id)->first();

        if (!$config) return $this->response([],'参数未配置',500);

        $config->delete();

        return $this->response([],'删除成功');
    }
}
